package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.NewsDao;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.model.NewsModel;
import com.cn.wanxi.service.INewsService;
import com.cn.wanxi.util.IO;
import com.cn.wanxi.util.Path;
import com.cn.wanxi.util.PathEnum;
import com.cn.wanxi.util.Tool;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class NewsServiceImpl implements INewsService {
    @Override
    public List<NewsModel> getNewsModelListForHomePage() {
        //        1.创建文件,魔法数据

        File file = IO.createFile(PathEnum.PATH.getPath(), PathEnum.FILE_NAME_NEWS.getPath());
//        2.写入数据
        List<String> array = getDaoData();
        IO.write(array, file);
//        3.读取数据
        String result = IO.readNote(file);
        System.out.println(result);
        List<NewsModel> newsModelList = setNavModelList(result);
        return newsModelList;
    }

    public static void main(String[] args) {

        System.out.println(PathEnum.PATH.getPath());
    }

    private List<NewsModel> setNavModelList(String result) {
        List<NewsModel> list = new ArrayList<>();
        String[] results = result.split(Path.SPLIT);
        String title = Tool.replace(results[0]);
        String[] titles = title.split(",");
        String content = Tool.replace(results[1]);
        String[] contents = content.split(",");
        int length = titles.length;
        for (int i = 0; i < length; i++) {
            NewsModel model = new NewsModel();
            model.setTitle(titles[i]);
            model.setContent(contents[i]);
            list.add(model);
        }
        return list;
    }


    private List<String> getDaoData() {
        List<String> list = new ArrayList<>();
        NewsDao newsDao = new NewsDao();
        list.add(Arrays.toString(newsDao.titlesForHomePage));
        list.add(Arrays.toString(newsDao.contentsForHomePage));
        return list;
    }
}
